﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;



public partial class LoginEN : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        GlobalVars.setPersonsInSession();

        if (Session["CurrentPerson"] != null)
        {
            try
            {
                Persons current = (Persons)Session["CurrentPerson"];
                if (current.lstAllRulesIDs.Contains((long)RulesEnum.Home_page_view))
                    Response.Redirect("./HomeEN.aspx");

            }
            catch
            {
            }
        }


        if (!Page.IsPostBack)
        {
            Page.DataBind();
        }
    }

    //protected void cv1_ServerValidate(object source, ServerValidateEventArgs args)
    //{
    //    if (txtBadge.Text.Trim() != "" && txtID.Text.Trim() != "")
    //    {
    //        args.IsValid = false;
    //        return;
    //    }

    //    if (txtBadge.Text.Trim() == "" && txtID.Text.Trim() == "")
    //    {
    //        args.IsValid = false;
    //        return;
    //    }

    //    args.IsValid = true;
        
    //}
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            PersonsDB objPersonsDB = new PersonsDB(ConnectionStrings.LMS);
            Persons objPersons = new Persons();
            //if (txtBadge.Text.Trim() != "")
            //{
                objPersons.Badge = txtBadge.Text.Trim();
            //}
            //else if (txtID.Text.Trim() != "")
            //{
            //    objPersons.ID = txtID.Text.Trim();
                
            //}

            
           

            objPersons.Password =  LMSSecurity.HashPassword(txtPassword.Text.Trim());

            objPersons.StatusID = 1;


            List<Persons> lstPersons = objPersonsDB.Search(objPersons);
            //if (txtID.Text.Trim() == "")
            //{
                lstPersons = new List<Persons>(from obj in lstPersons where obj.ID == "" select obj);
            //}
            if (lstPersons.Count > 0)
            {
                Session["CurrentPerson"] = lstPersons[0];

                if (chkRemeber.Checked)
                {
                    HttpCookie hc = new HttpCookie ("CurrentPerson");
                    hc.Expires = DateTime.Now.AddYears (1);
                    hc.Value = lstPersons[0].GenID.ToString();

                    Response.Cookies.Add(hc);
                }

                Response.Redirect("HomeEN.aspx");
            }
            else
            {

                objPersons = new Persons();
               
                objPersons.ID = txtBadge.Text.Trim();
               




                objPersons.Password = LMSSecurity.HashPassword(txtPassword.Text.Trim());

                objPersons.StatusID = 1;


                 lstPersons = objPersonsDB.Search(objPersons);

                if (lstPersons.Count > 0)
                {
                    Session["CurrentPerson"] = lstPersons[0];

                    if (chkRemeber.Checked)
                    {
                        HttpCookie hc = new HttpCookie("CurrentPerson");
                        hc.Expires = DateTime.Now.AddYears(1);
                        hc.Value = lstPersons[0].GenID.ToString();

                        Response.Cookies.Add(hc);
                    }

                    Response.Redirect("HomeEN.aspx");
                }
                else
                {
                    lblError.Text = "Badge or Password is wrong.";
                }

                
            }
        }
    }
}
